package com.nuoniu.sibanyun.mapper.crm.call;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.annotation.DataAuth;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallTask;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallTaskDto;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallTaskUpdateDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 客户管理-外呼任务管理 Mapper 接口
 * </p>
 *
 * @author 白强
 * @since 2022-02-28
 */
public interface CrmCallTaskMapper extends BaseMapper<CrmCallTask> {
    /**
     * 分页查询
     * @param crmCallTaskDto
     * @return
     */
    @DataAuth(tableAlias = "t",personalField = "create_user_id",companyField = "company_id",officeField = "office_id")
    Page<CrmCallTask> page(Page page,@Param("dto") CrmCallTaskDto crmCallTaskDto,Integer companyId,Integer userId);

    /**
     * 批量删除(逻辑删除)
     * @param ids
     */
    int batchDelete(List<Long> ids,Integer companyId,Integer userId);


    /**
     * 批量关闭 任务
     * @param ids
     */
    int closeTask(List<Long> ids,Integer companyId,Integer userId);

    void updateTaskMobileCount(@Param("dto") CrmCallTaskUpdateDto crmCallTaskUpdateDto);

    /**
     * 系统校验 全盘 表记录 超时未完成的任务
     */
    @Update("UPDATE crm_call_task set `status` = 3 WHERE end_time < CURDATE()  AND business_status = 0 AND mobile_count > dialed_count AND `status` < 2")
    int checkUnfinishedTask();


    @Select("SELECT id FROM crm_call_task  WHERE end_time < CURDATE() AND business_status = 0")
    List<Long> listTaskIds();

    @Update("UPDATE crm_call_task SET business_status = 1 WHERE id IN (${taskIds})")
    int updateTaskState(String taskIds);

    @Update("UPDATE pub_customer SET is_tasking = 0,crm_task_id = 0 WHERE crm_task_id IN (${taskIds})")
    int updateCrmCustorTaskState(String taskIds);

    @Update("UPDATE crm_linkman SET is_tasking = 0,crm_task_id = 0 WHERE crm_task_id IN (${taskIds})")
    int updateLinkManTaskState(String taskIds);

    @Update("UPDATE crm_call_task SET status = #{status} WHERE id = #{id}")
    int updateTaskStatusById(Long id,Integer status);


}
